// https://www.baidu.com/link?url=1o1cok1I3cftRhhyOGEwKANL3mgR_KXaZNj8wCmfBY7wcNntKMUx40aXM-jIEeAL&wd=&eqid=f3cb4015000cabf8000000065f3c7c87
import React, { useState, useEffect } from "react";

export default function useRequest(func) {
  const [loading, setLoading] = useState(false);
  const [result, setResult] = useState<unknown>({});

  useEffect(() => {
    // 自执行函数
    (async () => {
      setLoading(true);
      try {
        const res = await func();
        setResult(res);
      } catch (err) {
        console.log("err--------->>>", err);
      } finally {
        setLoading(false);
      }
    })();
  }, []);

  return {
    loading,
    result,
  };
}
